import { request } from "../unit/request.js"
import { onMounted, computed, reactive, provide, ref, toRefs } from "vue"
import { listAll } from "@/api/tabel.js"
import { useCounterStore } from "@/pinia/index.js"
import { containsProp } from "@vueuse/shared"
const store = useCounterStore()


//加载数据接口封装完成
export function loading(onlyId) {
    return listAll('', '', onlyId).then(data => {
        onlyId = data.data
        localStorage.setItem(onlyId, JSON.stringify(data.data))
        return onlyId
    })
}



//删除数据封装完成
export function Delete(sj, id) {
    const Id = sj.text.id
    return listAll("", "", id).then(data => {
        return data.data
    })
        .then(data => {
            data.filter(item => {
                if (item.id === Id) {
                    listAll("", "DELETE", id + "/" + item.id)
                }
            })
        })
        .then(() => {
            return listAll("", "", id)
                .then(data => {
                    // console.log(data)
                    return data.data
                })
        })
}


//修改数据封装完成
export function xiugai(e, id) {
    //获取当前行的ID
    const onlyid = e.text.id
    //当前行修改的数据
    const spform = store.spform
    //提交
    return listAll(spform, 'PATCH', id + "/" + onlyid)
        .then(() => {
            return listAll("", "", id)
                .then(data => {
                    // console.log(data)
                    return data.data
                })
        })
}


//添加表格数据
export const piniaADD = computed(() => {
    return store.spform
})

//执行表格函数  
function addList(pass, onlysj) {
    listAll(pass, "POST", onlysj).then(data => {
        tabelsj.value.push(data.data)
        localStorage.setItem("splist", JSON.stringify(tabelsj))
    })
    store.$state = { spform: {} }
}

